<template>
  <div class="app-container">
    <div class="head">
      <img src="@/assets/img/Layout/position.png" />
      <p class="see-block">产品详情库存查询</p>
    </div>
    <div class="view-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="仓库" prop="warehouseName">
          <el-select
            v-model="queryParams.warehouseName"
            clearable
            size="small"
            style="width: 200px;"
            @change="val => warehouseData(val)"
          >
            <el-option
              v-for="item in warehouseList"
              :key="item.name"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="specificationName">
          <el-input
            v-model="queryParams.specificationName"
            placeholder="请输入产品名称"
            clearable
            size="small"
            style="width:230px;"
            @keyup.enter.native="handleQuery"
            @clear="handleClear"
          />
        </el-form-item>
        <el-form-item label="批次" prop="batchNumber">
          <el-input
            v-model="queryParams.batchNumber"
            placeholder="请输入批次号"
            clearable
            size="small"
            style="width:230px;"
            @keyup.enter.native="handleQuery"
            @clear="handleClear"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="list-search btn-success" color="#5ED4D4">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="list-search">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="list" border>
        <el-table-column label="序号" type="index" align="center" width="50">
           <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
          </template>
        </el-table-column>
        <el-table-column label="产品编码" align="center" prop="productCode" :show-overflow-tooltip="true" width="180" />
        <el-table-column label="产品名称" align="center" prop="productName" :show-overflow-tooltip="true" width="180" />
        <el-table-column label="SKU编码" align="center" prop="specificationCode" :show-overflow-tooltip="true" width="180" />
        <el-table-column label="SKU名称" align="center" prop="specificationName" :show-overflow-tooltip="true" width="200" />
        <el-table-column label="规格" align="center" :show-overflow-tooltip="true" width="150">
          <template slot-scope="scope">
              {{ scope.row.value1 == '' || scope.row.value1 == null ? '' : scope.row.value1 + scope.row.value2 }}
          </template>
        </el-table-column>
        <el-table-column label="批次号" align="center" prop="batchNumber" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="库存数量" align="center" prop="store" :show-overflow-tooltip="true" width="100" />
        <el-table-column label="计量单位" align="center" prop="productUnit" :show-overflow-tooltip="true" width="100" />
        <el-table-column label="仓库" align="center" prop="warehouseName" :show-overflow-tooltip="true" width="120"/>
        <el-table-column label="区域" align="center" prop="areaName" :show-overflow-tooltip="true" />
        <el-table-column label="仓位" align="center" prop="positionName" :show-overflow-tooltip="true" />
        <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { productStock } from "@/api/system/stock/stock"
import { queryWarehouse } from "@/api/system/warehouse/warehouse"

export default {
  name: "stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      total: 0,
      list: [],
      warehouseList: [], // 仓库列表
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseName: undefined,
        warehouseId: undefined,
        specificationName: undefined,
        batchNumber: undefined
      },
    }
  },
  created() {
    this.getList()
    this.getWarehouseList()
  },
  methods: {
    // 获取列表
    getList() {
      this.loading = true
      productStock(this.addDateRange(this.queryParams)).then((res) => {
        this.list = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 获取仓库列表
    getWarehouseList(){
      queryWarehouse().then(res => {
        if(res.code == 200){
          this.warehouseList = res.data
        }
      })
    },
    warehouseData(val){
      if(val){
        let data = this.warehouseList.find(item => item.name == val)
        this.queryParams.warehouseName = data.name
        this.queryParams.warehouseId = data.warehouseId
      }else{
        this.queryParams.warehouseName = undefined
        this.queryParams.warehouseId = undefined
      }
      this.handleQuery()
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置按钮
    resetQuery() {
      this.queryParams.warehouseId = undefined
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 清除
    handleClear(){
      this.handleQuery()
    },
  }
}
</script>
